package com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.front.sbsslustandmultselect_frm.voactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.builtincomponents.idgen.UUID32;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;

import java.util.ArrayList;
import java.util.UUID;

public class addStandToPlanVOAction extends AbstractFSAction<VoidActionResult> {

	private ArrayList<String> planIds;
	private ArrayList<String> standIds;
	private String rhjhCfg = "com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.back.sbssSbrhJh";
	private String sbtzCfg = "com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.SbssSbtz";
	private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);
	public addStandToPlanVOAction(ArrayList<String> planIds,ArrayList<String> standIds) {

		this.planIds = planIds;
		this.standIds = standIds;
	}
	@Override
	public void execute() {
		bqlExecuter.getOptions().setOptionType(OptionType.ExecuteSql);

		IStandardLcp tzLcp = LcpUtil.getLcp(sbtzCfg);
		for (String planId : planIds) {
			int i = addStandToPlan(planId, standIds);
			if (i > 0) {
				ModifyChangeDetail devmcd = new ModifyChangeDetail(planId);
				devmcd.putItem("rhp", true);
				tzLcp.modify(devmcd);
			}
		}
		tzLcp.save();
	}


	private int addStandToPlan(String devId,ArrayList<String> standIds) {
		int count = 0;
		for (String standId : standIds){
			StringBuilder insetSql = new StringBuilder("INSERT INTO SBSS_SBTZ_RHBZMX (ID, PARENTID, LUSTAND)");
			String id = new UUID32().generateId();
			insetSql.append(" SELECT '")
					.append(id)
					.append("', '")
					.append(devId)
					.append("', '")
					.append(standId)
					.append("' FROM DUAL WHERE NOT EXISTS ( SELECT 1 FROM SBSS_SBTZ_RHBZMX  WHERE PARENTID = '")
					.append(devId)
					.append("' AND LUSTAND = '")
					.append(standId)
					.append("')");
			count += bqlExecuter.executeBqlStatement(insetSql.toString(),new ArrayList<>());
		}
		return count;
	}
}
